package com.oy.topline.sqlite

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper


class SQLiteHelper(context: Context?) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
    override fun onCreate(db: SQLiteDatabase) {
        /**
         * 创建用户信息表
         */
        db.execSQL(
            "CREATE TABLE  IF NOT EXISTS " + U_USERINFO + "( "
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + "userName VARCHAR, " //用户名
                    + "nickName VARCHAR, " //昵称
                    + "sex VARCHAR, " //性别
                    + "signature VARCHAR," //签名
                    + "head VARCHAR " //头像
                    + ")"
        )
        /**
         * 创建十二星座信息表
         */
        db.execSQL(
            "CREATE TABLE  IF NOT EXISTS " + CONSTELLATION + "( "
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + "c_id INT, " //星座id
                    + "name VARCHAR, " //星座名称
                    + "head VARCHAR, " //头像
                    + "img VARCHAR," //图标
                    + "icon VARCHAR," //白色图标
                    + "date VARCHAR," //日期
                    + "info VARCHAR," //星座信息
                    + "whole INT," //整体运势
                    + "love INT," //爱情运势
                    + "career INT," //事业学业
                    + "money INT," //财富运势
                    + "whole_info VARCHAR," //整体运势信息
                    + "love_info VARCHAR," //爱情运势信息
                    + "career_info VARCHAR," //事业学业信息
                    + "money_info VARCHAR," //财富运势信息
                    + "health_info VARCHAR" //健康运势信息
                    + ")"
        )
        /**
         * 创建收藏表
         */
        db.execSQL(
            "CREATE TABLE  IF NOT EXISTS " + COLLECTION_NEWS_INFO + "( "
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + "id INTEGER, " //新闻id
                    + "type INTEGER, " //新闻类型
                    + "userName VARCHAR," //用户名
                    + "newsName VARCHAR, " //新闻名称
                    + "newsTypeName VARCHAR," //新闻类型名称
                    + "img1 VARCHAR, " //图片1
                    + "img2 VARCHAR, " //图片2
                    + "img3 VARCHAR, " //图片3
                    + "newsUrl VARCHAR " //新闻链接地址
                    + ")"
        )
    }

    /**
     * 当数据库版本号增加时才会调用此方法
     */
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.execSQL("DROP TABLE IF EXISTS " + U_USERINFO)
        db.execSQL("DROP TABLE IF EXISTS " + CONSTELLATION)
        db.execSQL("DROP TABLE IF EXISTS " + COLLECTION_NEWS_INFO)
        onCreate(db)
    }

    companion object {
        private const val DB_VERSION = 1
        var DB_NAME = "topline.db"
        const val U_USERINFO = "userinfo" //用户信息
        const val CONSTELLATION = "constellation" //十二星座信息

        //收藏新闻信息
        const val COLLECTION_NEWS_INFO = "collection_news_info"
    }
}